package com.rocket.bedroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rocket.bedroom.pojo.ClassInfoEntity;
import com.rocket.bedroom.service.IClassInfoService;
import com.rocket.bedroom.service.impl.ClassInfoServiceImpl;
import org.springframework.cglib.core.ClassInfo;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 18:12 2020/5/9
 * @ModifyBy:
 */
@RestController
@RequestMapping(value = "/class")
public class ClassInfoController {

    @Resource
    private IClassInfoService classInfoService;

    @GetMapping(value = "/pageClassInfo")
    public PageInfo<ClassInfoEntity> pageClassInfo(ClassInfoEntity classInfoEntity, Integer pageNumber, Integer pageSize) {
        if (null == pageNumber || 0 >= pageNumber) {
            pageNumber = 1;
        }
        if (null == pageSize || 0 >= pageSize) {
            pageSize = 30;
        }
        PageHelper.startPage(pageNumber, pageSize);


        LambdaQueryWrapper<ClassInfoEntity> lambdaQuery = Wrappers.lambdaQuery();
        if (null != classInfoEntity) {
            if (!StringUtils.isEmpty(classInfoEntity.getClassName())) {
                lambdaQuery.likeRight(ClassInfoEntity::getClassName, classInfoEntity.getClassName());
            }
            if (null != classInfoEntity.getGradeId() && 0 != classInfoEntity.getGradeId()) {
                lambdaQuery.eq(ClassInfoEntity::getGradeId, classInfoEntity.getGradeId());
            }
        }
        List<ClassInfoEntity> list = classInfoService.list(lambdaQuery);
        return new PageInfo<>(list);
    }

    @GetMapping(value = "/getClassInfoEntityTest")
    public IPage<ClassInfoEntity> getClassInfoEntityTest() {
        return classInfoService.page(new Page<>(1, 10));
    }

    @GetMapping(value = "/hasClassInfo")
    public int hasClassInfo(ClassInfoEntity classInfoEntity) {
        return classInfoService.getClassInfoCount(classInfoEntity);
    }


    @GetMapping(value = "/findClassInfo/{classInfoId}")
    public ClassInfoEntity findClassInfo(@PathVariable(value = "classInfoId") Long classInfoId) {
        Assert.notNull(classInfoId, "ClassInfo Id IS NULL");
        return classInfoService.getById(classInfoId);
    }

    @PostMapping(value = "/addClassInfo")
    public int addClassInfo(ClassInfoEntity classInfoEntity) {
        Assert.notNull(classInfoEntity, "ADD ClassInfo IS NULL");
        Assert.notNull(classInfoEntity.getClassName(), "ADD ClassInfo Name IS NULL");
        Assert.notNull(classInfoEntity.getGradeId(), "ADD ClassInfo GradeId IS NULL");
        Assert.notNull(classInfoEntity.getClaTeacher(), "ADD ClassInfo Teacher IS NULL");
        Assert.isTrue(
                0 == classInfoService.getClassInfoCount(classInfoEntity)
                , "班级名称存在重复");
        boolean saveClass = classInfoService.save(classInfoEntity);
        return saveClass ? 1 : -1;
    }


    @PutMapping(value = "/modifyClassInfo")
    public int modifyClassInfo(ClassInfoEntity classInfoEntity) {
        Assert.notNull(classInfoEntity, "UPDATE ClassInfo IS NULL");
        Assert.notNull(classInfoEntity.getClassName(), "ADD ClassInfo Name IS NULL");
        Assert.notNull(classInfoEntity.getGradeId(), "ADD ClassInfo GradeId IS NULL");
        Assert.notNull(classInfoEntity.getClaTeacher(), "ADD ClassInfo Teacher IS NULL");
        Assert.isTrue(
                0 == classInfoService.getClassInfoCount(classInfoEntity)
                , "班级名称存在重复");
        boolean updateClass = classInfoService.updateById(classInfoEntity);
        return updateClass ? 1 : -1;
    }


    @DeleteMapping(value = "/delClassById/{classInfoId}")
    public int delClassById(@PathVariable("classInfoId") Long classInfoId) {
        Assert.notNull(classInfoId, "DELETE ClassInfoId ID IS NULL");
        boolean delClass = classInfoService.removeById(classInfoId);
        //班级中是否存在学生校验
        return delClass ? 1 : -1;
    }

}
